<template>
    <div class="xtx-goods-page" v-if="goods.categories">
        <div class="container">
            <!-- 面包屑 -->
            <xtx-bread>
                <xtx-bread-item to="/">首页</xtx-bread-item>
                <xtx-bread-item :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}
                </xtx-bread-item>
                <xtx-bread-item :to="`/subCategory/${goods.categories[0].id}`">{{goods.categories[0].name}}
                </xtx-bread-item>
                <xtx-bread-item>{{goods.name}}</xtx-bread-item>
            </xtx-bread>
            <!-- 商品信息 -->
            <div class="goods-info">
                <!-- 图片预览区 -->
                <div class="media">
                    <GoodsImage :images="goods.mainPictures" />
                    <!-- 销售信息 -->
                    <GoodsSales :good="goods" />
                </div>
                <!-- 商品信息区 -->
                <div class="spec">
                    <!-- 信息组件 -->
                    <GoodsName :good="goods" />
                    <!-- sku -->
                    <GoodsSku :goods="goods" @change="skuChange" />
                    <!-- 按钮 -->
                    <XtxButton type="primary" style="margin-top:20px;" @click="insertCart">加入购物车</XtxButton>
                </div>
            </div>
            <!-- 商品详情 -->
            <div class="goods-footer">
                <div class="goods-article">
                    <!-- 商品+评价 -->
                    <div class="goods-tabs">
                        <GoodsTabs />
                    </div>
                </div>
                <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
            </div>
        </div>
    </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './Sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
// import { Message } from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const route = useRoute()
    const goods = ref({})
    async function getGoods () {
      const res = await findGoods(route.params.id)
      goods.value = res.result
    }
    onMounted(() => {
      getGoods()
    })
    // sku变化处理数据
    const currSku = ref(null) // 当前用户选择的商品规格组合sku
    function skuChange (sku) {
      // 如果传出来的值有skuId 证明是有效数据
      if (sku.skuId) {
        goods.value.price = sku.price // 价格
        goods.value.oldPrice = sku.oldPrice // 老价格
        goods.value.inventory = sku.inventory // 库存
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }
    // 加入购物车逻辑
    const num = ref(1) // 商品数量
    const store = useStore()
    const insertCart = () => {
      if (!currSku.value) {
        // Message({ type: 'success', text: '添加购物车成功' })
        return false
      }
      if (num.value > goods.value.inventory) {
        console.log('123')
        return false
      }
      store.dispatch('cart/insertCart', {
        id: goods.value.id,
        skuId: currSku.value.skuId,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: currSku.value.price,
        nowPrice: currSku.value.price,
        count: num.value,
        attrsText: currSku.value.specsText,
        selected: true,
        isEffective: true,
        stock: currSku.value.inventory
      }).then(() => {
        // 提示用户 当前已经添加成功
        // Message({ type: 'success', text: '添加购物车成功' })
      })
    }
    return { goods, skuChange, num, insertCart }
  }
}
</script>

<style scoped lang='scss'>
  .goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
